package com.lsh.oms.worker.service.base;

import com.dangdang.ddframe.job.api.JobExecutionMultipleShardingContext;
import com.dangdang.ddframe.job.plugin.job.type.simple.AbstractSimpleElasticJob;
import com.lsh.base.trace.id.filter.TraceIdSetter;
import com.lsh.oms.core.email.SendEmailService;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.service.po.OmsTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Slf4j
public abstract class OmsSimpleElasticJob extends AbstractSimpleElasticJob {

    @Autowired
    private OmsTaskService omsTaskService;

    @Autowired
    private SendEmailService sendEmailService;

    @Override
    public void process(JobExecutionMultipleShardingContext context) {
        TraceIdSetter.set();
        try {
            List<OmsTask> omsTaskList = this.fetchOmsTaskList();
            if (omsTaskList == null) {
                return;
            }
            log.info("[omsTaskList size is]  {},taskType {}", omsTaskList.size(), this.getTaskType().getCode());

            for (OmsTask task : omsTaskList) {

                Integer execCount = task.getExecCount();
                if (execCount >= 10) {
                    continue;
                }

                execCount++;

                int processFlag;
                try {
                    processFlag = this.process(task);
                } catch (Exception e) {
                    log.error("[omsTask异常] type = " + task.getTaskType() + ", refCode = " + task.getRefCode(), e);
                    processFlag = -1;
                }
                //更新数据库
                OmsTask omsTask = new OmsTask();
                omsTask.setId(task.getId());
                omsTask.setExecCount(execCount);
                Integer taskStatus = TaskStatus.TASK_DEAL_SUCCESS.getCode();
                if (processFlag == -1) {
                    taskStatus = TaskStatus.TASK_DEAL_FAIL.getCode();
                }
                omsTask.setTaskStatus(taskStatus);
                if (processFlag != 0) {
                    this.omsTaskService.update(omsTask);
                } else {
                    log.info("task type {},task id {},暂不需要处理", task.getTaskType(), task.getRefCode());
                }

//                if (processFlag == -1 && task.getExecCount() > 8) {
//
////                    this.sendEmailService.asyncSendEmail("[" + task.getRefCode() + "]任务处理失败 type [" + task.getTaskType() + "]", "oms-task处理失败(" + DateUtil.formatTime(new Date()) + ")");
//                }
            }
        } catch (Exception ex) {

            log.error("omsTask异常", ex);
        }
    }

    protected List<OmsTask> fetchOmsTaskList() {

        return omsTaskService.getTasksByTypeAndStatuses(this.getTaskType().getCode(), this.dealTaskStatus(), this.getFetchSize());
    }

    protected abstract Integer getFetchSize();

    protected abstract TaskType getTaskType();

    protected abstract List<Integer> dealTaskStatus();

    protected abstract int process(OmsTask omsTask);

}
